class Heap {
    upAdjust(arr) {
        let childIndex = arr.length - 1;
        let parentIndex = Math.floor((arr.length - 1) / 2);
        const temp = arr[childIndex];
        while (childIndex > 0 && temp < arr[parentIndex]) {
            arr[childIndex] = arr[parentIndex];
            childIndex = parentIndex;
            parentIndex = Math.floor((parentIndex - 1) / 2);
        }
        arr[childIndex] = temp;
    }
    downAdjust(arr, parentIndex) {
        const length = arr.length;
        const temp = arr[parentIndex];
        let childIndex = 2 * parentIndex + 1;
        while (childIndex < length) {
            if (childIndex + 1 < length && arr[childIndex + 1] < arr[childIndex]) {
                childIndex++;
            }
            if (temp < arr[childIndex]) {
                break;
            }
            arr[parentIndex] = arr[childIndex];
            parentIndex = childIndex;
            childIndex = 2 * parentIndex + 1;
        }
        arr[parentIndex] = temp;
    }
    buildHeap(arr) {
        for (let i = Math.floor((arr.length - 2) / 2); i >= 0; i--) {
            this.downAdjust(arr, i);
        }
    }
}

const heap = new Heap();
let arr = [1,3,2,6,5,7,8,9,10,0];
heap.upAdjust(arr);
console.log(arr);

arr = [7,1,3,10,5,2,8,9,6];
heap.buildHeap(arr);
console.log(arr);